package homework;
//作业标题(2302)
//获取链表的中间节点
//
//作业内容
//实现链表题目: 获取链表的中间节点
//https://leetcode.cn/problems/middle-of-the-linked-list/description/
public class h2302 {
    class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode middleNode(ListNode head) {
        if(head == null || head.next == null) return head;

        ListNode fast = head;
        ListNode last = head;
        while(fast != null && fast.next != null)
        {
            fast = fast.next.next;
            last = last.next;
        }
        return last;
    }
}
